先在根目錄建立 modules/功能名稱 資料夾。
把 Module 及相關原生程式碼檔案移至這個資料夾。
建立 index.js
跟 package.json
檔案。
First, let's modify your index.js
file in the modules/notification_progress
directory to add some debug logging:
import { NativeModulesProxy, EventEmitter } from 'expo-modules-core';
console.log('NativeModulesProxy:', NativeModulesProxy);
console.log('NotificationProgressModule:', NativeModulesProxy.NotificationProgressModule);
const NotificationProgressModule = NativeModulesProxy.NotificationProgressModule;
export function buildNotification() {
console.log('Attempting to call buildNotification');
if (NotificationProgressModule && NotificationProgressModule.buildNotification) {
return NotificationProgressModule.buildNotification();
} else {
console.error('buildNotification is not available');
return 'Error: buildNotification is not available';
}
}
export default NotificationProgressModule;
NotificationProgressModule.kt
) is correctly implementing the function:package expo.modules.notificationprogress
import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition
class NotificationProgressModule : Module() {
override fun definition() = ModuleDefinition {
Name("NotificationProgressModule")
Function("buildNotification") {
// Your implementation here
"Notification built from Kotlin!" // Return some value
}
}
}
import { buildNotification } from 'notification_progress';
try {
console.log('Imported module:', buildNotification);
const result = buildNotification();
console.log('Result:', result);
} catch (error) {
console.error('Error calling buildNotification:', error);
}
app.json
or app.config.js
is correctly referencing the module:{
"expo": {
"plugins": [
"modules/notification_progress"
]
}
}
npx expo prebuild --clean
android/app/src/main/java/com/your_project_name/MainApplication.java
(or .kt
if it's Kotlin). Make sure there's a line adding your package to the list of packages:List<ReactPackage> packages = new PackageList(this).getPackages();
// ... potentially other packages
packages.add(new expo.modules.notificationprogress.NotificationProgressPackage());
If this line is missing, Expo might not be recognizing your module correctly.
npx expo run:android
If you're still encountering issues, it could be due to a few reasons:
At this point, it would be helpful to see:
android/settings.gradle
and android/app/build.gradle
files.With this information, we can better diagnose why the module isn't being recognized properly.